RNN for pictures genaration

This notebook is an experiment. I tryed to generate a picture pixel by pixel using an RNN.
each pixel can be black or white. WIP

Import needed for Jupiter


In [38]:
%matplotlib notebook
import matplotlib
import matplotlib.pyplot as plt

from IPython.display import Image

Import needed for the code


In [39]:
import numpy as np
import tensorflow as tf

import fnmatch, os
import time

Helpers functions

to save a picture


In [40]:
#need to be called within a session
def write_png(tensor, name):
    casted_to_uint8 = tf.cast(tensor, tf.uint8)
    converted_to_png = tf.image.encode_png(casted_to_uint8)
    f = open(name, "wb+")
    f.write(converted_to_png.eval())
    f.close()

A class to define all args


In [42]:
class Args():
    def __init__(self):
        '''directory to store checkpointed models'''
        self.save_dir = 'save_face_training_v0.3'
        
        '''Picture size'''
        self.picture_size = 32
    
        '''size of RNN hidden state'''
        self.rnn_size = self.picture_size*3 
        '''minibatch size'''
        self.batch_size = 1
        '''RNN sequence length'''
        self.seq_length = self.picture_size
        '''number of epochs'''
        self.num_epochs = 10 # was 5
        '''save frequency'''
        self.save_every = 100 # was 500
        '''Print frequency'''
        self.print_every = 10
        '''clip gradients at this value'''
        self.grad_clip = 5.
        '''learning rate'''
        self.learning_rate = 0.002 # was 0.002
        '''decay rate for rmsprop'''
        self.decay_rate = 0.98
        """continue training from saved model at this path.
        Path must contain files saved by previous training process: """
        #self.init_from = 'save_face_training'
        self.init_from = None
        
        '''number of ligne to sample'''
        self.n = 250

In [43]:
class FaceLoader:
    def prepare_reading_faces(self):
        self.matches = []
    
        for root, dirnames, filenames in os.walk('./lfw/'):
            #print filenames
            for filename in fnmatch.filter(filenames, '*.jpg'):
                self.matches.append(os.path.join(root, filename))

        size = len(self.matches)

        filenames = tf.constant(self.matches)
        self.filename_queue = tf.train.string_input_producer(filenames)
        self.image_reader = tf.WholeFileReader()
        return size
    
    def do_when_session(self):   
        # For some reason, we need a coordinator and some threads
        self.coord = tf.train.Coordinator()
        self.threads = tf.train.start_queue_runners(coord=self.coord)

    def stop_reading_faces(self):
        # Finish off the filename queue coordinator.
        self.coord.request_stop()
        self.coord.join(self.threads)
              
    def load_one_face(self, image_size):
        # read and decode image, will give a uint8 with shape [250, 250, 1]
        filename, image_file = self.image_reader.read(self.filename_queue)     
        image = tf.image.decode_jpeg(image_file, channels=1)
        #resize
        image = tf.image.resize_images(image, image_size, image_size)

        # remove channel dimension
        tensor_uint8 = tf.squeeze(image, squeeze_dims=[2])

        # convert to float32 and scale
        face = tf.cast(tensor_uint8, tf.float32)/255.0
        self.picture = tf.constant(face.eval())
        #print self.picture
      
    def get_bw_picts(self, level):      
        bw = (tf.sign(self.picture-level)+1)/2
        #print bw.eval()
        return bw
            
    def get_training_set():
        xdata = a_vector_face.eval()
        ydata = np.copy(xdata)
        ydata[:-1] = xdata[1:]
        ydata[-1] = xdata[0]
        self.x_batches = np.squeeze(np.split(xdata, image_size, 0))
        self.y_batches = np.squeeze(np.split(ydata, image_size, 0))
                  
    def next_batch(self):
        return self.x_batches, self.y_batches

This code to that the formulas are working.

It create a list of pictures
Useless for now.


In [53]:
'''
tf.reset_default_graph()
faceloader = FaceLoader()
face_count = faceloader.prepare_reading_faces()
with tf.Session() as sess:
    tf.initialize_all_variables().run()
    faceloader.do_when_session()
    faceloader.load_one_face(250)
    for i in range(255/2):
        bw = faceloader.get_bw_picts(i*2/255.)
        bw = tf.expand_dims(bw, 2)
        write_png(bw*255., "generated{:06}.png".format(i))
'''


Out[53]:
'\ntf.reset_default_graph()\nfaceloader = FaceLoader()\nface_count = faceloader.prepare_reading_faces()\nwith tf.Session() as sess:\n    tf.initialize_all_variables().run()\n    faceloader.do_when_session()\n    faceloader.load_one_face(250)\n    for i in range(255/2):\n        bw = faceloader.get_bw_picts(i*2/255.)\n        bw = tf.expand_dims(bw, 2)\n        write_png(bw*255., "generated{:06}.png".format(i))\n'

In [54]:
'''
from PIL import Image, ImageSequence
import glob, sys, os
os.chdir(".")
frames = []
for file in glob.glob("gene*.png"):
    print(file)
    im = Image.open(file)
    frames.append(im)

from images2gif import writeGif
writeGif("generated.gif", frames, duration=0.1)
'''


Out[54]:
'\nfrom PIL import Image, ImageSequence\nimport glob, sys, os\nos.chdir(".")\nframes = []\nfor file in glob.glob("gene*.png"):\n    print(file)\n    im = Image.open(file)\n    frames.append(im)\n\nfrom images2gif import writeGif\nwriteGif("generated.gif", frames, duration=0.1)\n'

In [55]:
class Model():
    def __init__(self, args, infer=False):
        self.args = args
        #if infer:
        #    '''Infer is true when the model is used for sampling'''
        #    args.seq_length = 1
   
        hidden_size = args.rnn_size
        vector_size = args.picture_size
        
        # define place holder to for the input data and the target.
        self.input_data = tf.placeholder(tf.float32, [ args.seq_length, vector_size], name='input_data')
        self.target_data = tf.placeholder(tf.float32, [ args.seq_length, vector_size], name='target_data') 
        # define the input xs
        xs = tf.split(0, args.seq_length, self.input_data)
        # define the target
        targets = tf.split(0, args.seq_length, self.target_data)  
        #initial_state
        self.initial_state = tf.zeros((hidden_size,1))
        #last_state = tf.placeholder(tf.float32, (hidden_size, 1))
        
        # model parameters
        Wxh = tf.Variable(tf.random_uniform((hidden_size, vector_size))*0.01, name='Wxh') # input to hidden
        Wph = tf.Variable(tf.random_uniform((hidden_size, vector_size))*0.01, name='Wph') # position to hidden
        Whh = tf.Variable(tf.random_uniform((hidden_size, hidden_size))*0.01, name='Whh') # hidden to hidden
        Why = tf.Variable(tf.random_uniform((vector_size, hidden_size))*0.01, name='Why') # hidden to output
        bh = tf.Variable(tf.zeros((hidden_size, 1)), name='bh') # hidden bias
        by = tf.Variable(tf.zeros((vector_size, 1)), name='by') # output bias
        loss = tf.zeros([1], name='loss')
        self.pos = tf.Variable(0.0, trainable=False, name='pos')
        hs, ys, ps = {}, {}, {}
        
        hs[-1] = self.initial_state
        # forward pass                                                                                                                                                                              
        for t in xrange(args.seq_length):
            xs_t = tf.transpose(xs[t])
            if infer and t>0:
                xs_t = ys[t-1]
            targets_t = tf.transpose(targets[t])
            indices = [[t, 0]]
            values = [1.0]
            shape = [args.seq_length, 1]
            delta = tf.SparseTensor(indices, values, shape) 
            position = tf.zeros([vector_size, 1]) + tf.sparse_tensor_to_dense(delta)
            
            hs[t] = tf.sigmoid(tf.matmul(Wxh, xs_t) 
                               + tf.matmul(Whh, hs[t-1]) 
                               + tf.matmul(Wph, position)
                               + bh) # hidden state
            ys[t] = tf.matmul(Why, hs[t]) + by # unnormalized log probabilities for next line
            ys[t] = tf.sigmoid(ys[t])
            #ps[t] = tf.exp(ys[t]) / tf.reduce_sum(tf.exp(ys[t])) # probabilities for next chars
            loss += tf.reduce_sum(tf.abs(ys[t]-targets_t))
                

        self.probs = tf.pack([ys[key] for key in ys])
        self.cost = loss / args.batch_size / args.seq_length
        self.final_state = hs[args.seq_length-1]
        self.lr = tf.Variable(0.0, trainable=False, name='learning_rate')
        tvars = tf.trainable_variables()
        grads, _ = tf.clip_by_global_norm(tf.gradients(self.cost, tvars),
                args.grad_clip)
        optimizer = tf.train.AdamOptimizer(self.lr)
        self.train_op = optimizer.apply_gradients(zip(grads, tvars))

    def sample(self, sess):
        size = self.args.picture_size
        picture_vect = np.zeros((size, size))
        state = model.initial_state.eval()
        x = np.random.random([1, size])
        feed = {self.input_data: x, self.initial_state:state}
        [probs, state] = sess.run([self.probs, self.final_state], feed)
        for n in range(0):
            line = np.transpose(probs)
            feed = {self.input_data: line, self.initial_state:state}
            [probs, state] = sess.run([self.probs, self.final_state], feed)
        for n in range(size):
            line = np.transpose(probs)
            feed = {self.input_data: line, self.initial_state:state}
            [probs, state] = sess.run([self.probs, self.final_state], feed)
            #print probs
            #line = (np.sign(probs-0.5)+1)/2
            #print line
            picture_vect[n] = np.squeeze(line)  
            picture = picture_vect*255
        return tf.expand_dims(picture,2)
    
    def inspect(self, draw=False):
        for var in tf.all_variables():
            if var in tf.trainable_variables():
                print ('t', var.name, var.eval().shape)
                if draw:
                    plt.figure(figsize=(1,1))
                    plt.figimage(var.eval())
                    plt.show()
            else:
                print ('nt', var.name, var.eval().shape)

Training


In [ ]:
tf.reset_default_graph()
args = Args()
model = Model(args)
print ("model created")
faceloader = FaceLoader()
face_count = faceloader.prepare_reading_faces()
print ('faces count', face_count)

cost_optimisation = []

with tf.Session() as sess:
    tf.initialize_all_variables().run()
    print ("variable initialized")
    faceloader.do_when_session()
    saver = tf.train.Saver(tf.all_variables())

    # restore model
    if args.init_from is not None:
        ckpt = tf.train.get_checkpoint_state(args.init_from)
        assert ckpt,"No checkpoint found"
        assert ckpt.model_checkpoint_path,"No model path found in checkpoint"
        saver.restore(sess, ckpt.model_checkpoint_path)
        print ("model restored")
    for e in range(args.num_epochs):
        faceloader.image_reader.reset()
        sess.run(tf.assign(model.lr, args.learning_rate * (args.decay_rate ** e)))
        state = model.initial_state.eval()
        for b in range(face_count):
            start = time.time()
            # Get learning data
            faceloader.load_one_face(args.picture_size)
            x, y = faceloader.next_batch()
            # Create the structure for the learning data
            feed = {model.input_data: x, model.target_data: y, model.initial_state: state}
            # Run a session using train_op
            [train_loss], state, _ = sess.run([model.cost, model.final_state, model.train_op], feed)
            end = time.time()
            if (e * face_count + b) % args.print_every == 0:
                cost_optimisation.append(train_loss)
                print("{}/{} (epoch {}), train_loss = {:.6f}, time/batch = {:.3f}" \
                    .format(e * face_count + b,
                            args.num_epochs * face_count,
                            e, train_loss, end - start))
            if (e * face_count + b) % args.save_every == 0:
                checkpoint_path = os.path.join(args.save_dir, 'model.ckpt')
                saver.save(sess, checkpoint_path, global_step = e * face_count + b)
                print("model saved to {}".format(checkpoint_path))
                np.save('cost', cost_optimisation)


model created
('faces count', 13233)
variable initialized
0/132330 (epoch 0), train_loss = 11.846004, time/batch = 0.299
model saved to save_face_training/model.ckpt
10/132330 (epoch 0), train_loss = 7.609998, time/batch = 0.150
20/132330 (epoch 0), train_loss = 7.214982, time/batch = 0.150
30/132330 (epoch 0), train_loss = 4.345308, time/batch = 0.148
40/132330 (epoch 0), train_loss = 7.447813, time/batch = 0.154
50/132330 (epoch 0), train_loss = 7.482661, time/batch = 0.154
60/132330 (epoch 0), train_loss = 6.279762, time/batch = 0.153
70/132330 (epoch 0), train_loss = 7.785715, time/batch = 0.156
80/132330 (epoch 0), train_loss = 4.964209, time/batch = 0.161
90/132330 (epoch 0), train_loss = 7.774941, time/batch = 0.163
100/132330 (epoch 0), train_loss = 10.457413, time/batch = 0.162
model saved to save_face_training/model.ckpt
110/132330 (epoch 0), train_loss = 7.914732, time/batch = 0.165
120/132330 (epoch 0), train_loss = 7.111222, time/batch = 0.167
130/132330 (epoch 0), train_loss = 7.023408, time/batch = 0.168
140/132330 (epoch 0), train_loss = 4.848790, time/batch = 0.169
150/132330 (epoch 0), train_loss = 7.302919, time/batch = 0.171
160/132330 (epoch 0), train_loss = 3.674625, time/batch = 0.174
170/132330 (epoch 0), train_loss = 8.263390, time/batch = 0.176
180/132330 (epoch 0), train_loss = 6.758085, time/batch = 0.182
190/132330 (epoch 0), train_loss = 3.910988, time/batch = 0.179
200/132330 (epoch 0), train_loss = 5.194339, time/batch = 0.180
model saved to save_face_training/model.ckpt
210/132330 (epoch 0), train_loss = 4.551933, time/batch = 0.183
220/132330 (epoch 0), train_loss = 6.324289, time/batch = 0.190
230/132330 (epoch 0), train_loss = 5.959601, time/batch = 0.186
240/132330 (epoch 0), train_loss = 7.450030, time/batch = 0.188
250/132330 (epoch 0), train_loss = 5.124949, time/batch = 0.195
260/132330 (epoch 0), train_loss = 4.154919, time/batch = 0.200
270/132330 (epoch 0), train_loss = 4.003494, time/batch = 0.196
280/132330 (epoch 0), train_loss = 5.118635, time/batch = 0.205
290/132330 (epoch 0), train_loss = 4.427326, time/batch = 0.205
300/132330 (epoch 0), train_loss = 4.765638, time/batch = 0.208
model saved to save_face_training/model.ckpt
310/132330 (epoch 0), train_loss = 3.735344, time/batch = 0.211
320/132330 (epoch 0), train_loss = 4.571231, time/batch = 0.206
330/132330 (epoch 0), train_loss = 5.801054, time/batch = 0.207
340/132330 (epoch 0), train_loss = 5.453507, time/batch = 0.210
350/132330 (epoch 0), train_loss = 6.524479, time/batch = 0.214
360/132330 (epoch 0), train_loss = 4.466758, time/batch = 0.218
370/132330 (epoch 0), train_loss = 3.093597, time/batch = 0.215
380/132330 (epoch 0), train_loss = 6.006961, time/batch = 0.220
390/132330 (epoch 0), train_loss = 5.414852, time/batch = 0.221
400/132330 (epoch 0), train_loss = 7.497438, time/batch = 0.225
model saved to save_face_training/model.ckpt
410/132330 (epoch 0), train_loss = 6.339993, time/batch = 0.224
420/132330 (epoch 0), train_loss = 6.619037, time/batch = 0.230
430/132330 (epoch 0), train_loss = 5.751145, time/batch = 0.228
440/132330 (epoch 0), train_loss = 5.581079, time/batch = 0.231
450/132330 (epoch 0), train_loss = 11.383739, time/batch = 0.233
460/132330 (epoch 0), train_loss = 5.102082, time/batch = 0.233
470/132330 (epoch 0), train_loss = 4.909813, time/batch = 0.243
480/132330 (epoch 0), train_loss = 7.428552, time/batch = 0.245
490/132330 (epoch 0), train_loss = 6.681951, time/batch = 0.240
500/132330 (epoch 0), train_loss = 5.431161, time/batch = 0.245
model saved to save_face_training/model.ckpt
510/132330 (epoch 0), train_loss = 6.323401, time/batch = 0.248
520/132330 (epoch 0), train_loss = 6.324689, time/batch = 0.255
530/132330 (epoch 0), train_loss = 5.522001, time/batch = 0.254
540/132330 (epoch 0), train_loss = 4.788240, time/batch = 0.255
550/132330 (epoch 0), train_loss = 5.925033, time/batch = 0.259
560/132330 (epoch 0), train_loss = 7.541386, time/batch = 0.259
570/132330 (epoch 0), train_loss = 4.742616, time/batch = 0.270
580/132330 (epoch 0), train_loss = 3.831331, time/batch = 0.262
590/132330 (epoch 0), train_loss = 8.389158, time/batch = 0.278
600/132330 (epoch 0), train_loss = 6.273591, time/batch = 0.268
model saved to save_face_training/model.ckpt
610/132330 (epoch 0), train_loss = 5.904160, time/batch = 0.270
620/132330 (epoch 0), train_loss = 7.140617, time/batch = 0.268
630/132330 (epoch 0), train_loss = 5.809810, time/batch = 0.267
640/132330 (epoch 0), train_loss = 5.787280, time/batch = 0.272
650/132330 (epoch 0), train_loss = 8.121449, time/batch = 0.278
660/132330 (epoch 0), train_loss = 3.951409, time/batch = 0.281
670/132330 (epoch 0), train_loss = 5.007551, time/batch = 0.280
680/132330 (epoch 0), train_loss = 5.078654, time/batch = 0.277
690/132330 (epoch 0), train_loss = 4.589311, time/batch = 0.291
700/132330 (epoch 0), train_loss = 4.262353, time/batch = 0.295
model saved to save_face_training/model.ckpt
710/132330 (epoch 0), train_loss = 4.878027, time/batch = 0.284
720/132330 (epoch 0), train_loss = 5.099174, time/batch = 0.287
730/132330 (epoch 0), train_loss = 8.126123, time/batch = 0.292
740/132330 (epoch 0), train_loss = 4.916740, time/batch = 0.291
750/132330 (epoch 0), train_loss = 4.964235, time/batch = 0.293
760/132330 (epoch 0), train_loss = 5.020784, time/batch = 0.297
770/132330 (epoch 0), train_loss = 5.582267, time/batch = 0.301
780/132330 (epoch 0), train_loss = 3.607513, time/batch = 0.298
790/132330 (epoch 0), train_loss = 4.957433, time/batch = 0.304
800/132330 (epoch 0), train_loss = 8.756006, time/batch = 0.303
model saved to save_face_training/model.ckpt
810/132330 (epoch 0), train_loss = 5.832938, time/batch = 0.314
820/132330 (epoch 0), train_loss = 7.171200, time/batch = 0.310
830/132330 (epoch 0), train_loss = 5.898413, time/batch = 0.322
840/132330 (epoch 0), train_loss = 4.798182, time/batch = 0.318
850/132330 (epoch 0), train_loss = 3.033494, time/batch = 0.326
860/132330 (epoch 0), train_loss = 4.715693, time/batch = 0.323
870/132330 (epoch 0), train_loss = 4.410551, time/batch = 0.449
880/132330 (epoch 0), train_loss = 3.671957, time/batch = 0.340
890/132330 (epoch 0), train_loss = 3.211787, time/batch = 0.410
900/132330 (epoch 0), train_loss = 5.762773, time/batch = 0.347
model saved to save_face_training/model.ckpt
910/132330 (epoch 0), train_loss = 6.225863, time/batch = 0.348
920/132330 (epoch 0), train_loss = 6.778830, time/batch = 0.416
930/132330 (epoch 0), train_loss = 4.799048, time/batch = 0.447
940/132330 (epoch 0), train_loss = 4.508432, time/batch = 0.343
950/132330 (epoch 0), train_loss = 6.060311, time/batch = 0.364
960/132330 (epoch 0), train_loss = 5.704145, time/batch = 0.363
970/132330 (epoch 0), train_loss = 6.299800, time/batch = 0.376
980/132330 (epoch 0), train_loss = 4.064652, time/batch = 0.376
990/132330 (epoch 0), train_loss = 5.421255, time/batch = 0.351
1000/132330 (epoch 0), train_loss = 3.249725, time/batch = 0.374
model saved to save_face_training/model.ckpt
1010/132330 (epoch 0), train_loss = 5.766055, time/batch = 0.370
1020/132330 (epoch 0), train_loss = 8.624304, time/batch = 0.368
1030/132330 (epoch 0), train_loss = 3.750180, time/batch = 0.365
1040/132330 (epoch 0), train_loss = 5.541049, time/batch = 0.368
1050/132330 (epoch 0), train_loss = 5.823425, time/batch = 0.366
1060/132330 (epoch 0), train_loss = 3.467629, time/batch = 0.382
1070/132330 (epoch 0), train_loss = 4.423463, time/batch = 0.396
1080/132330 (epoch 0), train_loss = 7.298230, time/batch = 0.418
1090/132330 (epoch 0), train_loss = 6.507596, time/batch = 0.405
1100/132330 (epoch 0), train_loss = 4.531975, time/batch = 0.421
model saved to save_face_training/model.ckpt
1110/132330 (epoch 0), train_loss = 3.758112, time/batch = 0.430
1120/132330 (epoch 0), train_loss = 3.868862, time/batch = 0.475
1130/132330 (epoch 0), train_loss = 7.370373, time/batch = 0.409
1140/132330 (epoch 0), train_loss = 4.399141, time/batch = 0.415
1150/132330 (epoch 0), train_loss = 4.341646, time/batch = 0.395
1160/132330 (epoch 0), train_loss = 4.806339, time/batch = 0.402
1170/132330 (epoch 0), train_loss = 4.351828, time/batch = 0.431
1180/132330 (epoch 0), train_loss = 3.155379, time/batch = 0.403
1190/132330 (epoch 0), train_loss = 5.923162, time/batch = 0.412
1200/132330 (epoch 0), train_loss = 3.506185, time/batch = 0.429
model saved to save_face_training/model.ckpt
1210/132330 (epoch 0), train_loss = 4.326102, time/batch = 0.413
1220/132330 (epoch 0), train_loss = 5.531622, time/batch = 0.421
1230/132330 (epoch 0), train_loss = 7.466490, time/batch = 0.407
1240/132330 (epoch 0), train_loss = 5.292984, time/batch = 0.434
1250/132330 (epoch 0), train_loss = 9.296682, time/batch = 0.442
1260/132330 (epoch 0), train_loss = 7.542732, time/batch = 0.463
1270/132330 (epoch 0), train_loss = 5.754212, time/batch = 0.416
1280/132330 (epoch 0), train_loss = 6.197461, time/batch = 0.441
1290/132330 (epoch 0), train_loss = 4.116388, time/batch = 0.452
1300/132330 (epoch 0), train_loss = 6.148061, time/batch = 0.456
model saved to save_face_training/model.ckpt
1310/132330 (epoch 0), train_loss = 4.099864, time/batch = 0.446
1320/132330 (epoch 0), train_loss = 4.672244, time/batch = 0.461
1330/132330 (epoch 0), train_loss = 4.554517, time/batch = 0.463
1340/132330 (epoch 0), train_loss = 4.426936, time/batch = 0.456
1350/132330 (epoch 0), train_loss = 8.222431, time/batch = 0.448
1360/132330 (epoch 0), train_loss = 2.372349, time/batch = 0.474
1370/132330 (epoch 0), train_loss = 4.806358, time/batch = 0.453
1380/132330 (epoch 0), train_loss = 2.799022, time/batch = 0.478
1390/132330 (epoch 0), train_loss = 3.600282, time/batch = 0.461
1400/132330 (epoch 0), train_loss = 5.314572, time/batch = 0.487
model saved to save_face_training/model.ckpt
1410/132330 (epoch 0), train_loss = 3.716839, time/batch = 0.474
1420/132330 (epoch 0), train_loss = 5.867580, time/batch = 0.502
1430/132330 (epoch 0), train_loss = 3.307088, time/batch = 0.483
1440/132330 (epoch 0), train_loss = 5.661429, time/batch = 0.524
1450/132330 (epoch 0), train_loss = 5.752923, time/batch = 0.488
1460/132330 (epoch 0), train_loss = 5.262812, time/batch = 0.516
1470/132330 (epoch 0), train_loss = 4.028398, time/batch = 0.525
1480/132330 (epoch 0), train_loss = 3.386873, time/batch = 0.508
1490/132330 (epoch 0), train_loss = 7.146141, time/batch = 0.537
1500/132330 (epoch 0), train_loss = 2.592293, time/batch = 0.503
model saved to save_face_training/model.ckpt
1510/132330 (epoch 0), train_loss = 4.420721, time/batch = 0.540
1520/132330 (epoch 0), train_loss = 6.661725, time/batch = 0.538
1530/132330 (epoch 0), train_loss = 3.700504, time/batch = 0.534
1540/132330 (epoch 0), train_loss = 6.506403, time/batch = 0.519
1550/132330 (epoch 0), train_loss = 4.536848, time/batch = 0.544
1560/132330 (epoch 0), train_loss = 5.640853, time/batch = 0.551
1570/132330 (epoch 0), train_loss = 5.638056, time/batch = 0.520
1580/132330 (epoch 0), train_loss = 7.173485, time/batch = 0.541
1590/132330 (epoch 0), train_loss = 2.604686, time/batch = 0.538
1600/132330 (epoch 0), train_loss = 3.820972, time/batch = 0.534
model saved to save_face_training/model.ckpt
1610/132330 (epoch 0), train_loss = 3.754754, time/batch = 0.554
1620/132330 (epoch 0), train_loss = 2.470329, time/batch = 0.541
1630/132330 (epoch 0), train_loss = 5.454981, time/batch = 0.544
1640/132330 (epoch 0), train_loss = 3.191897, time/batch = 0.556
1650/132330 (epoch 0), train_loss = 3.515225, time/batch = 0.558
1660/132330 (epoch 0), train_loss = 2.818658, time/batch = 0.554
1670/132330 (epoch 0), train_loss = 3.251315, time/batch = 0.559
1680/132330 (epoch 0), train_loss = 4.379750, time/batch = 0.575
1690/132330 (epoch 0), train_loss = 6.107162, time/batch = 0.581
1700/132330 (epoch 0), train_loss = 10.520744, time/batch = 0.573
model saved to save_face_training/model.ckpt
1710/132330 (epoch 0), train_loss = 5.984098, time/batch = 0.576
1720/132330 (epoch 0), train_loss = 3.026716, time/batch = 0.578
1730/132330 (epoch 0), train_loss = 5.400842, time/batch = 0.577
1740/132330 (epoch 0), train_loss = 4.510511, time/batch = 0.601
1750/132330 (epoch 0), train_loss = 5.325934, time/batch = 0.585
1760/132330 (epoch 0), train_loss = 5.739883, time/batch = 0.597
1770/132330 (epoch 0), train_loss = 2.015807, time/batch = 0.606
1780/132330 (epoch 0), train_loss = 2.713206, time/batch = 0.585
1790/132330 (epoch 0), train_loss = 5.520914, time/batch = 0.639
1800/132330 (epoch 0), train_loss = 2.586047, time/batch = 0.644
model saved to save_face_training/model.ckpt
1810/132330 (epoch 0), train_loss = 5.679832, time/batch = 0.645
1820/132330 (epoch 0), train_loss = 3.191137, time/batch = 0.657
1830/132330 (epoch 0), train_loss = 3.479776, time/batch = 0.647
1840/132330 (epoch 0), train_loss = 6.601597, time/batch = 0.668
1850/132330 (epoch 0), train_loss = 3.503648, time/batch = 0.658
1860/132330 (epoch 0), train_loss = 3.836609, time/batch = 0.663
1870/132330 (epoch 0), train_loss = 3.950723, time/batch = 0.663
1880/132330 (epoch 0), train_loss = 5.474760, time/batch = 0.691
1890/132330 (epoch 0), train_loss = 4.600953, time/batch = 0.659
1900/132330 (epoch 0), train_loss = 5.153743, time/batch = 0.668
model saved to save_face_training/model.ckpt
1910/132330 (epoch 0), train_loss = 4.155967, time/batch = 0.638
1920/132330 (epoch 0), train_loss = 3.450524, time/batch = 0.714
1930/132330 (epoch 0), train_loss = 3.735923, time/batch = 0.656
1940/132330 (epoch 0), train_loss = 3.744927, time/batch = 0.642
1950/132330 (epoch 0), train_loss = 3.671499, time/batch = 0.744
1960/132330 (epoch 0), train_loss = 3.720388, time/batch = 0.656
1970/132330 (epoch 0), train_loss = 5.815224, time/batch = 0.670
1980/132330 (epoch 0), train_loss = 3.059415, time/batch = 0.672
1990/132330 (epoch 0), train_loss = 3.465191, time/batch = 0.677
2000/132330 (epoch 0), train_loss = 2.620191, time/batch = 0.690
model saved to save_face_training/model.ckpt
2010/132330 (epoch 0), train_loss = 4.495060, time/batch = 0.685

In [ ]:
cost_optimisation = np.load('cost.npy')
plt.figure(figsize=(12,5))
plt.plot(range(len(cost_optimisation)), cost_optimisation, label='cost')
plt.legend()
plt.show()

In [ ]:
tf.reset_default_graph()
args = Args()
model = Model(args, True)  # True to generate the model in sampling mode
with tf.Session() as sess:
    tf.initialize_all_variables().run()
    saver = tf.train.Saver(tf.all_variables())
    ckpt = tf.train.get_checkpoint_state(args.save_dir)
    print (ckpt)
    
    model.inspect(draw=True)

sampling


In [10]:
tf.reset_default_graph()
args = Args()
model = Model(args, infer=True)
with tf.Session() as sess:
    tf.initialize_all_variables().run()
    print 'intialisation done'
    saver = tf.train.Saver(tf.all_variables())
    ckpt = tf.train.get_checkpoint_state(args.save_dir)
    print (ckpt)
    
    if ckpt and ckpt.model_checkpoint_path:
        saver.restore(sess, ckpt.model_checkpoint_path)
               
    state = model.initial_state.eval()
    x = np.random.random([args.picture_size, args.picture_size])
    feed = {model.input_data: x, model.initial_state: state}
    [lines] = sess.run([model.probs], feed)
    pict = tf.expand_dims(lines*255,2)
    #print(pict.eval())
    write_png(lines*255, 'a_face.png')


intialisation done
model_checkpoint_path: "save_face_training/model.ckpt-2000"
all_model_checkpoint_paths: "save_face_training/model.ckpt-1600"
all_model_checkpoint_paths: "save_face_training/model.ckpt-1700"
all_model_checkpoint_paths: "save_face_training/model.ckpt-1800"
all_model_checkpoint_paths: "save_face_training/model.ckpt-1900"
all_model_checkpoint_paths: "save_face_training/model.ckpt-2000"


In [11]:
Image("a_face.png")


Out[11]:

Feedback wellcome @dh7net